package bb.lanxing.activity

import android.content.DialogInterface
import android.text.TextUtils
import android.view.LayoutInflater
import android.widget.EditText
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import bb.lanxing.App
import bb.lanxing.R
import bb.lanxing.databinding.ActivitySpeedAlertSettingBinding
import bb.lanxing.manager.SharedManager
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.mvvm.sportsetting.SportSettingViewModel
import bb.lanxing.mvvm.sportsetting.UserSportSetting
import bb.lanxing.util.ui.InputHelper
import bb.lanxing.view.BiciAlertDialogBuilder
import kotlin.jvm.internal.Intrinsics
import kotlin.math.abs

class SpeedAlertSettingActivity : BaseDBActivity<ActivitySpeedAlertSettingBinding>() {
    private var speedAlert = 0
    override fun getLayoutId() = R.layout.activity_speed_alert_setting

    private val viewModel by lazy {
        ViewModelProvider(this)[SportSettingViewModel::class.java]
    }

    override fun initView(binding: ActivitySpeedAlertSettingBinding) {
        setupActionBar(true)
        setTitle(R.string.device_xoss_g_speed_warn)
        binding.warningValueLayout.setOnClickListener {
            showSpeedAlertDialog(speedAlert)
        }
        viewModel.getGetSettingResult().observe(this, object : Observer<UserSportSetting> {
            override fun onChanged(value: UserSportSetting) {
                val speedAlarm = value.speedAlarm

                if (speedAlarm != null) {
                    if (speedAlarm == 0) {
                        binding.warningSwitch.isChecked = false
                        return
                    }
                    binding.warningSwitch.isChecked = true
                    speedAlert = speedAlarm
                    if (speedAlarm <= 0) {
                        return
                    }
                    binding.txtSpeedAlert.text = speedAlert.toString()
                }
            }
        })
        binding.warningSwitch.setOnCheckedChangeListener { _, z ->
            if (!z) {
                speedAlert = 0
                updateSpeedAlert(0)
            }
        }
        viewModel.getUpdateSettingResult()
            .observe(this, Observer<UserSportSetting> { userSportSetting ->
                SharedManager.getInstance().setDeviceSpeedAlert(speedAlert)
                val speedAlarm = userSportSetting.speedAlarm
                if (speedAlarm != null) {
                    if (speedAlarm == 0) {
                        return@Observer
                    }
                    speedAlert = speedAlarm
                    if (speedAlarm <= 0) {
                        return@Observer
                    }
                    binding.txtSpeedAlert.text = speedAlert.toString()
                }
            })
    }

    private fun showSpeedAlertDialog(speedAlert: Int) {
        val speedAlertSettingActivity = this
        val inflate =
            LayoutInflater.from(speedAlertSettingActivity).inflate(R.layout.edittext_view, null)
        val editText = inflate.findViewById<EditText>(R.id.editText)
        editText.inputType = 4098
        editText.maxLines = 1
        if (speedAlert == 0) {
            editText.setHint(R.string.dialog_hint_input)
        } else {
            editText.setText(speedAlert.toString())
        }
        val dialog =
            BiciAlertDialogBuilder(speedAlertSettingActivity).setView(inflate).setPositiveButton(
                R.string.dialog_btn_ok
            ) { dialogInterface: DialogInterface, i2: Int ->
                updateSpeedAlertValue(
                    editText, speedAlert, dialogInterface, i2
                )
            }.setNegativeButton(R.string.dialog_btn_cancel) { _, _ ->
                ignoreSpeedUpdate(
                    editText
                )
            }
        dialog.setTitle(R.string.setting_speed_warn_title)
        dialog.show()
        InputHelper.showSoftInput(editText)
    }


    override fun onResume() {
        super.onResume()
        viewModel.getSportSetting(App.getContext().userId)
    }

    private fun updateSpeedAlert(i: Int) {
        viewModel.updateSportSetting(
            App.getContext().userId, UserSportSetting(i, null, null, null, null, null)
        )
    }

    private fun updateSpeedAlertValue(
        editText: EditText, i: Int, dialogInterface: DialogInterface, i2: Int
    ) {
        val obj = editText.text.toString()
        if (TextUtils.isEmpty(obj) || Intrinsics.areEqual(obj, i.toString())) {
            dialogInterface.cancel()
        } else {
            var abs = abs(obj.toInt())
            if (abs > 100) {
                snack(R.string.st_speed_too_high)
                abs = 100
            }
            if (abs < 15) {
                snack(R.string.st_speed_too_low)
                abs = 15
            }
            speedAlert = abs
            binding.txtSpeedAlert.text = speedAlert.toString()
            binding.warningSwitch.isChecked = true
            updateSpeedAlert(abs)
        }
        InputHelper.hideSoftInput(editText)
    }

    private fun ignoreSpeedUpdate(editText: EditText) {
        InputHelper.hideSoftInput(editText)
    }
}